package activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.jaden.washeradministrator.ActivityManagement;
import com.example.jaden.washeradministrator.MyApplication;
import com.example.jaden.washeradministrator.R;
import com.example.jaden.washeradministrator.ResponseMessage;
import com.example.jaden.washeradministrator.TransferData;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;

import admin.Admin;
import okhttp3.MediaType;

public class LoginPage extends AppCompatActivity implements View.OnClickListener {
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    Button Login;
    Button Register;
    EditText Username;
    EditText Password;
    String username;
    String password;
    boolean is_valid=false;
    MyApplication application;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginpage);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);

        //获取Toolbar的实例
        Toolbar toolbar=(Toolbar)findViewById(R.id.loginpage_toolbar);
        setSupportActionBar(toolbar);


        //获取控件实例
        getInstance();
        //执行按键点击监听
        setClickListener();
    }
    protected void onDestroy() {
        ActivityManagement.KillOneActivity();
        super.onDestroy();
    }
    //获取控件实例
    private void getInstance() {

        Login=(Button)findViewById(R.id.button_login);
        Register=(Button)findViewById(R.id.button_register);
        Username=(EditText)findViewById(R.id.editText_login_username);
        Password=(EditText)findViewById(R.id.editText_login_password);
    }
    //执行按键点击监听
    private void setClickListener(){
        Login.setOnClickListener(this);
        Register.setOnClickListener(this);
    }
    public void verification(ResponseMessage responseMessage)
    {
        switch (responseMessage.getCode())
        {
            case 2006:
                Admin admin= (Admin) responseMessage.getData();
                application.setAdmin(admin);
                Intent intent = new Intent(LoginPage.this, HomePage.class);
                startActivity(intent);
                break;
            default:
                Toast.makeText(this,responseMessage.getMsg(),Toast.LENGTH_LONG).show();
                break;

        }
    }
    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId())
        {
            case R.id.button_login:
                Login.setEnabled(false);
                username=Username.getText().toString().trim();
                password=Password.getText().toString().trim();
                // String url="http://192.168.99.155:8080/Test/Demo";
                String url="http://47.96.159.236:8080/Wash/Login?method=Login";
                try {
                    //打包Json数据
                    JSONObject userJson=new JSONObject();
                    userJson.put("loginName",username);
                    userJson.put("password",password);
                    userJson.put("userIdentity",2);
                    String messageJson=String.valueOf(userJson);
                    TransferData.LoginAuthentication(url,messageJson);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                //获取全局变量 实例
                application=MyApplication.getInstance();
                while(true) {
                    if(application.isLoginOk()) {
                        application.setLoginOk(false);
                        ResponseMessage responseMessage =application.getResponseMessage();
                        verification(responseMessage);


                        break;
                    }
                }
                Login.setEnabled(true);
                break;
            case R.id.button_register:
                intent =new Intent(LoginPage.this, RegisterPage.class);
                startActivity(intent);
                break;
        }

    }


}
